package com.realshare.bby.service.integral;

import com.realshare.frame.component.quartz.base.AutoScheduler;
import com.realshare.frame.component.quartz.base.BaseScheduler;
import com.realshare.frame.util.DateUtils;
import com.realshare.frame.util.EasyHashMap;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 自动审核积分
 * author leisure
 * email 15075107120@139.com
 * create 2018-11-23 下午 5:54
 * modify 2018-11-23 下午 5:54
 **/
@Service
//自动注入定时任务，并指定任务名称
@AutoScheduler(name = "AutoAuditScheduler", group = "Integral")
//使用@DisallowConcurrentExecution设置不并发执行
@DisallowConcurrentExecution
public class AutoAuditScheduler extends BaseScheduler {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SQLManager sqlManager;

    public AutoAuditScheduler() {
        super(true);
    }

    @Override
    public String setDescription() {
        return "处理自动审核任务";
    }

    @Override
    protected void executeInternal(JobExecutionContext context) {
        //处理自动审核
        //查询三天前需要待审核的积分补贴记录
        log.info("开始处理待审核的积分补贴记录");
        String sql = "select v.id verifyId,t.id subsidyId,case when c.IDENTITY_CODE is NOT null then 1 else 0 end checkStatus" +
                ",c.IDENTITY_CODE  IDENTITY_CODE ,t.integral integral,t.org_code org_code ,t.create_time  create_time ,t.creator creator" +
                " from rule_subsidy_verisy v  " +
                "left join rule_integral_task t on v.user_subsidy_id = t.id " +
                "left join customer_means c on t.identity_code = c.IDENTITY_CODE  " +
                "where v.verisy_status = 0  and SUBSTR(t.create_time ,1, 10)<=date_sub(curdate(),interval 3 day) " ;
        // 1、查询需要处理的超时未兑换的订单
        log.info("查询需要处理的待审核的积分补贴记录" );
        List<EasyHashMap> mapList = sqlManager.execute(new SQLReady(sql), EasyHashMap.class);
        String now = DateUtils.formatLongDate(new Date());
        String upSql;
        for (EasyHashMap map : mapList) {
            String verifyId = map.getString("verifyId");//审核的记录id
            String subsidyId = map.getString("subsidyId");//积分补贴的记录id
            String checkStatus = map.getString("checkStatus");//核验的状态   0-用户不存在 1-用户存在
            String identity_code = map.getString("IDENTITY_CODE");//用户身份证号
            String integral = map.getString("integral");//用户身份证号
            String org_code =  map.getString("org_code");//添加积分补贴记录人的网点
            String create_time =  map.getString("create_time");//记录添加时间
            String creator =  map.getString("creator");//记录添加人
           if(checkStatus.equals("1")){//设置审核通过
              //1、补全审核记录信息
               upSql = "UPDATE rule_subsidy_verisy SET verisy_user_id = 'system' ,verisy_time =  '"+  now +"',  verisy_status = '1' ,verisy_describe = '服务系统审核通过'  WHERE ID='" + verifyId + "'";
               log.info("服务系统审核通过" );
               sqlManager.executeUpdate(new SQLReady(upSql));
               //2、设置积分补贴审核状态
               upSql = "UPDATE rule_integral_task SET status = 3 , DATA_TIME = '"+create_time.replaceAll("-","")+"' WHERE ID='" + subsidyId + "'";
               sqlManager.executeUpdate(new SQLReady(upSql));
               //3、修改用户积分
               upSql= "update CUSTOMER_MEANS set INTEGRAL = IFNULL(INTEGRAL,0) +"+Double.parseDouble(integral)+" where IDENTITY_CODE = '"+identity_code+"'";
               log.info("审核通过修改用户积分" );
               sqlManager.executeUpdate(new SQLReady(upSql));
               //4、插入增加积分补贴记录日志
               upSql = "INSERT INTO INTEGRAL_FLOW(ID, TYPE, TITLE, QUANTITY, DEAL_TYPE, IS_READ, CREATOR, CREATE_TIME, EDITOR, EDIT_TIME, STATUS, DATA_TIME, CLEAR_STATUS, IDENTITY_CODE,ORG_CODE) VALUES ('"+subsidyId+"', '1', '积分补贴', '"+integral+"', '1', '0', '"+creator+"', '"+now+"', NULL, NULL, '1', '"+create_time+"', '0', '"+identity_code+"','"+org_code+"')";
               log.info("插入自动审核积分补贴流水" );
               sqlManager.executeUpdate(new SQLReady(upSql));

           }else{//设置审核不通过
               //1、补全审核记录信息
               upSql = "UPDATE rule_subsidy_verisy SET verisy_user_id = 'system' ,verisy_time =  '"+  now +"',  verisy_status = '2' ,verisy_describe = '未匹配到相关用户，服务系统审核不通过'  WHERE ID='" + verifyId + "'";
               log.info("未匹配到相关用户，服务系统审核不通过" );
               sqlManager.executeUpdate(new SQLReady(upSql));
               //2、设置积分补贴审核状态
               upSql = "UPDATE rule_integral_task SET status = 14  WHERE ID='" + subsidyId + "'";
               sqlManager.executeUpdate(new SQLReady(upSql));
           }

        }
        log.info("结束处理自动审核积分补贴");
    }

}